<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Session;

//提货控制器

class UpGoods extends AdminBase
{
    public function index(){

        $map = input("param.");

        if( empty($map["status"]) ){
            $where = " p.status >= 0 ";
        }else{
            $where = " p.status = ".$map["status"];
        }


        if( !empty($map["start_time"]) ){

            $where .= " AND p.add_time >= ".strtotime($map["start_time"]);
        }
        if( !empty($map["end_time"]) ){
            if( empty($map["start_time"]) ){
                $time = time() - (60*60*24*7);
                $where .= " AND p.add_time >= ".$time;
            }
            $where .= " AND p.add_time <= ".strtotime($map["end_time"]);
        }

        if(!empty($map["ordernumber"])){
            $where = " p.ordernumber = '".$map["ordernumber"]."'";
        }

        $lists = Db::name('User')
            ->alias('u')
            ->join('up_goods p','u.id = p.user_id','right')
            ->join('goods g','g.id=p.goods_id')
            ->field("p.*,nickname,account,g.name")
            ->order('add_time desc')
            ->where($where)
            ->paginate();
        //print_r($lists);exit;
        $page = $lists->render();
        $this->assign('page', $page);
        $this->assign("list",$lists);
        if(empty($Arr)){
            $Arr['status'] = '';
        }
        $this->assign("Arr",$Arr);
        return $this->fetch();
    }


    public function edit(){
        $id = input('param.');

        $data = Db::name('up_goods')
            ->alias("po")
            ->join("goods g","g.id=po.goods_id")
            //->join("address a","a.id=po.address_id")
            ->join("user u","u.id=po.user_id")
            ->where("po.id=".$id['id'])
            ->field("u.nickname,u.account,g.name,po.*")
            ->find();
        //$addr = str_replace('>',' ',$data['details']);

        if(request()->isAjax()){
            $d = input('post.');
            //print_r($d);exit;
            $d['f_time'] = time();
            $d['status'] = 3;

            $dd = input('post.id');

            $result = Db::name('up_goods')->where('id',$dd)->update($d);
            if($result){
                $arr = [
                    'result' => 0,
                    'msg' => '发货成功',
                ];
            }else{
                $arr = [
                    'result' => 1007,
                    'msg' => '发货失败',
                ];
            }
            $this->json_return($arr);
        }

        $this->assign('data',$data);

        //$this->assign('addr',$addr);
        return $this->fetch();
    }

    public function detail(){
        $id = input('param.');

        $data = Db::name('up_goods')
            ->alias("po")
            ->join("goods g","g.id=po.goods_id")
            //->join("address a","a.id=po.address_id")
            ->join("user u","u.id=po.user_id")
            ->where("po.id=".$id['id'])
            ->field("u.nickname,u.account,g.name,po.*")
            ->find();
        //$addr = str_replace('>',' ',$data['details']);

        $this->assign('data',$data);

        //$this->assign('addr',$addr);
        return $this->fetch();
    }
}